import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testcrd:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/1_get_crd_list.yaml'))
    def test_get_crd_list(self, crdinfo):
        # 获取crd列表接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url']
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/1_get_crd_list_exception.yaml'))
    def test_get_crd_list_exception(self, crdinfo):
        # 获取crd列表接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url']
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/2_create_crd.yaml'))
    def test_create_crd(self, crdinfo):
        # 创建crd接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url']
        method = crdinfo['request']['method']
        data = crdinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        crdname = req.json()['metadata']['name']
        globals()['crdname'] = crdname
        group = req.json()['spec']['group']
        globals()['group'] = group
        plural = req.json()['spec']['names']['plural']
        globals()['plural'] = plural
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/2_create_crd_exception.yaml'))
    def test_create_crd_exception(self, crdinfo):
        # 创建crd接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url']
        method = crdinfo['request']['method']
        data = crdinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/3_get_specific_crd.yaml'))
    def test_get_specific_crd(self, crdinfo):
        # 获取指定crd接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/3_get_specific_crd_exception.yaml'))
    def test_get_specific_crd_exception(self, crdinfo):
        # 获取指定crd接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/4_update_crd.yaml'))
    def test_update_crd(self, crdinfo):
        # 更新指定crd接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        crdinfo['request']['data'] = globals()['updatereqbody']
        data = crdinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/5_update_section_crd_exception.yaml'))
    def test_update_section_crd_exception(self, crdinfo):
        # 部分更新指定crd的接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        data = crdinfo['request']['data']
        header = crdinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/6_get_cr_list.yaml'))
    def test_get_cr_list(self, crinfo):
        # 获取cr列表接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = (base_url + crinfo['request']['url']
               .replace('{group}', globals()['group']).replace('{plural}', globals()['plural']))
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/6_get_cr_list_exception.yaml'))
    def test_get_cr_list(self, crinfo):
        # 获取cr列表接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = (base_url + crinfo['request']['url']
               .replace('{group}', globals()['group']).replace('{plural}', globals()['plural']))
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/7_create_cr.yaml'))
    def test_create_cr(self, crinfo):
        # 创建cr接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default')
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        crname = req.json()['metadata']['name']
        globals()['crname'] = crname
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/7_create_cr_exception.yaml'))
    def test_create_cr_exception(self, crinfo):
        # 创建cr接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default')
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/8_get_specific_cr.yaml'))
    def test_get_specific_cr(self, crinfo):
        # 获取指定cr接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/8_get_specific_cr_exception.yaml'))
    def test_get_specific_cr_exception(self, crinfo):
        # 获取指定cr接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/crd_and_cr/9_update_cr.yaml'))
    def test_update_cr(self, crinfo):
        # 更新指定cr接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        crinfo['request']['data'] = globals()['updatereqbody']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/9_update_cr_exception.yaml'))
    def test_update_cr_exception(self, crinfo):
        # 更新指定cr接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        crinfo['request']['data'] = globals()['updatereqbody']
        data = crinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/10_update_section_cr.yaml'))
    def test_update_section_cr(self, crinfo):
        # 部分更新指定cr的接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        header = crinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/10_update_section_cr_exception.yaml'))
    def test_update_section_cr_exception(self, crinfo):
        # 部分更新指定cr的接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        data = crinfo['request']['data']
        header = crinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/11_delete_specific_cr.yaml'))
    def test_delete_specific_cr(self, crinfo):
        # 删除cr接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/11_delete_specific_cr_exception.yaml'))
    def test_delete_specific_cr(self, crinfo):
        # 删除cr接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crinfo['request']['url'].replace('{group}', globals()['group']).replace('{plural}', globals()[
            'plural']).replace('{namespace}', 'default').replace('{crname}', globals()['crname'])
        method = crinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/12_delete_specific_crd.yaml'))
    def test_delete_specific_crd(self, crdinfo):
        # 删除crd接口-正常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.crd_and_cr
    @pytest.mark.parametrize('crdinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/crd_and_cr/12_delete_specific_crd_exception.yaml'))
    def test_delete_specific_crd_exception(self, crdinfo):
        # 删除crd接口-异常
        base_url = datas.baseUrl + Testcrd.url_unify
        url = base_url + crdinfo['request']['url'].replace('{crdname}', globals()['crdname'])
        method = crdinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == crdinfo['validate'][0]['equals']['status_code']
